/*
 * Field.java
 *
 * Created on 2007. mC!rcius 12., 16:49
 *
 */

package laborhazi;

/**
 * Storage of FieldElements and administration of them ie:
 * remove dem, add dem, give dem back to identificate dem
 * @author nyuszik
 */
public class Field {
    private FieldElement[][] elements;
    private int size = 0;
    private StartField start = null;
    private FinishField finish = null;
    private boolean current = true;
    private int lemmingNum = 0;
    public Viewer viewer;
    //private int times = 5;
    
    /** 
     * Creates a new instance of Field 
     * @param size the size of the n*n map, so it's the n value
     */
    public Field(int s, Viewer v) {
        size = s;
        viewer = v;
        elements = new FieldElement[size][size];
        
        System.out.println("new field: " + size + " " + size);
    }
    public boolean getCurrent() {
    	
    	return current;
    }
    public void decLemming() {
    	lemmingNum = lemmingNum - 1;
    	System.out.println(lemmingNum);
    }
    public void incLemming() {
    	lemmingNum = lemmingNum + 1;
    }
    public int getLemmingNum() {
    	
    	return lemmingNum;
    }
    public int getSize() {
    	return size;
    }
    public void listLemming() {
    	for (int i = 0; i < size; ++i) {
    		for (int j = 0; j < size; ++j) {
    			if ((elements[i][j] instanceof Lemming)) {
    				System.out.println(elements[i][j].toString());
    			}
    		}
    	}
    }
    public Lemming getLemming(int lem) {
    	for (int i = 0; i < size; ++i) {
    		for (int j = 0; j < size; ++j) {
    			if ((elements[i][j] instanceof Lemming) && ((Lemming)elements[i][j]).getCount() == lem) {
    				return (Lemming)elements[i][j];
    			}
    		}
    	}
    	return null;
    }
    /**
     * blows a FieldElement
     * @param position the position of the FieldElement 
     */
    public void blow(int[] position) {
    	check(position).blow();
    	viewer.blow(position);
    }
    /**
     * puts a FieldElement type into given position on Field
     * @param fieldElement the element to be put on the map
     * @param position the position of the FieldElement 
     */
    public void put(FieldElement fieldElement, int[] position) {
    	if (check(position) != null) {
    		if (check(position) instanceof SetLemming) {
    			check(position).add(fieldElement);
    			if (fieldElement instanceof Lemming)
    				((Lemming)fieldElement).setPos(position);
    		} else if (check(position) instanceof laborhazi.Lemming) {
    			SetLemming setLemming = new SetLemming(this);
    			setLemming.setPos(position);
    			setLemming.add(check(position));
    			setLemming.add(fieldElement);
    			//remove(fieldElement, fieldElement.getPosition());
    			remove(check(position), check(position).getPosition());
    			((Lemming)fieldElement).setPos(position);
    			this.put(setLemming, position);
    		}
    	} else {
    		fieldElement.setPos(position);
    		elements[(position[0]-1)][(position[1]-1)] = fieldElement;
    		System.out.println("new " + fieldElement.toString());
    		if(fieldElement != null && fieldElement instanceof FinishField) {
    			finish = (FinishField)fieldElement;
    		}
    	}
    }
    /** 
     * checks FieldElement in a given position
     * @return the checked field elements reference
     * @param position the position of the element to be checked
     */
    public FieldElement check(int[] position) {
    	if (position[0] > 0 && position[0] < size + 1 && position[1] > 0 && position[1] < size + 1) {
    		return( elements[(position[0]-1)][(position[1]-1)] );
    	} else {
    		return( (new Stone(this)).setNomap() );
    	}
    }
    /** 
     * checks if there is any lemming left on the Field
     * @return it's TRUE only if the element is a Lemming
     * @param position the position of the Lemming to be checked
     */
    public boolean isLemming(int[] position) {  
        return false;
    }
    
    public void update(){
    	int i,j;
    	for (i = 0; i < size; ++i) {
    		for (j = 0; j < size; ++j) {
    			if (elements[i][j] != null) ((FieldElement)elements[i][j]).update(current);
    		}
    	}
    	current = !current;
    	return ;
    }
    
    /** 
     * removes a field element in a given position
     * @param fieldElement the field element to be removed
     * @param position the position of the selected element
     */
    public void remove(FieldElement fieldElement, int[] position) {
        System.out.println("removed " + fieldElement.toString());
        if (check(position) != null && !(fieldElement instanceof SetLemming) && check(position) instanceof SetLemming) {
        	check(position).remove(fieldElement);
        } else {
        	if (position[0] > 0 && position[0] < size + 1 && position[1] > 0 && position[1] < size + 1) {
        		/*if(elements[position[0]-1][position[1]-1] instanceof SetLemming)
        			System.err.println("TEST -> " + elements[position[0]-1][position[1]-1]);*/
        		elements[position[0]-1][position[1]-1] = null;
        	}
        }
    }
    public FinishField getFinish(){
    	return finish;
    }
    public StartField getStart(){
    	return start;
    }
    public void setViewer (Viewer v) {
    	viewer = v;
    }
    public void eraseSet(int[] position) {
    	elements[position[0]-1][position[1]-1].erase();
    	elements[position[0]-1][position[1]-1] = null;
    }
}
